/*
 * ChargeItemResource.cpp
 *
 *  Created on: 2013. 9. 11.
 *      Author: mega@gamevil.com
 */

#include "../User.h"
#include "ChargeItemResource.h"
#include <Database/Static.h>
#include "../NUtil/Pool.h"
#include "../NUtil/Utils.h"

namespace ProjectZ { namespace NResource {

ChargeItemResource::ChargeItemResource()
{
	pChargeAD	= new ChargeItemData;
	pChargeIOS	= new ChargeItemData;
}

ChargeItemResource::~ChargeItemResource()
{
	delete pChargeAD;
	delete pChargeIOS;
}

bool ChargeItemResource::LoadResource()
{
	if (false == pChargeAD->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pChargeIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	return true;
}

ChargeItemData::Flyweight * ChargeItemResource::GetFly(User * pUser, int seq)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(pUser->GetCompany(), pUser->GetSaleCode());

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pChargeAD->GetFly(seq);

	case NUtil::MarketType::IOS_KAKAO:
		return pChargeIOS->GetFly(seq);

	default:
		return NULL;
	}

	return NULL;
}

ChargeItemData * ChargeItemResource::GetDataPtr(User * pUser)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(pUser->GetCompany(), pUser->GetSaleCode());

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pChargeAD;

	case NUtil::MarketType::IOS_KAKAO:
		return pChargeIOS;

	default:
		return NULL;
	}

	return NULL;
}

ChargeItemData::~ChargeItemData()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<CHARGEMAP::value_type>);
}

void
ChargeItemData::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<CHARGEMAP::value_type>);
	clsFlyTree.clear();
}

bool ChargeItemData::LoadResource(int company, int saleCode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT * FROM resource_charge_item WHERE company=%d AND sale_cd=%d", company, saleCode);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		int tmpCompany, tmpSaleCode;

		ChargeItemData::Flyweight * pFly = new ChargeItemData::Flyweight;

		clsAuto->GetRecord() >> NATIONAL_CODE >> pFly->_SEQ >> tmpCompany >> tmpSaleCode
				>> pFly->_ITEM_NAME >> pFly->_ICON >> pFly->_TID >> pFly->_SHEET
				>> pFly->_CATEGORY >> pFly->_CLASSTYPE >> pFly->_POSITION >> pFly->_PACKAGE >> pFly->_QUANTITY
				>> pFly->_PRICE >> pFly->_VC_INDEX >> pFly->_DISCOUNT >> pFly->_EXPLAIN;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[pFly->_SEQ] = pFly;
	}

	return true;
}


ChargeItemData::Flyweight * ChargeItemData::GetFly(int seq)
{
	auto iter = clsFlyTree.find(seq);

	if (iter == clsFlyTree.end())
		return NULL;

	return iter->second;
}

const int ChargeItemData::size(int itemCateory, int classType)
{
	auto iter = clsFlyTree.begin();

	int itemCount = 0;

	for ( ; iter != clsFlyTree.end(); iter++)
	{
		if ( iter->second->_CATEGORY != itemCateory)
			continue;

		if ( iter->second->_POSITION < 0)
			continue;

		if ( iter->second->_CLASSTYPE >= 0 && iter->second->_CLASSTYPE != classType )
			continue;

		itemCount++;
	}

	return itemCount;
}

} /* NResource */ } /* ProjectZ */

